Control Structures


All but the simplest of programs will involve control structures. These are the if-then-else and looping contructs, which allow you to execute code conditionally, and execute code many times respectivly.

If-Then-Else

Salsa's if statement is identical to C. To illustrate its use, here's a program which get input from the user with the input() function, stores it in data, and then prints "zero!" on the screen if the data is the number zero:
def main()
{
	data = input();
	if (data == 0)
		println("zero!");
}

There are several points to notice. First, the condition data == 0 has parenthesis around it. This is not optional; you always needs parenthesis around the condition. Why are the parenthesis necessary? Because there is nothing to separate the condition from the statement to be executed, which in this case is println("zero!");. Often you will want more then one line to be executed or skipped as a result of an if statement. To group several lines together and treat them as one, surround the lines with braces. Therefore, this code does the same thing as our previous example, but also prints "you typed:":
def main()
{
	data = input();
	if (data == 0) {
		println("you typed:");
		println("zero!");
	}
}

Also notice the way the condition is constructed with double-equals. This is necessary because the single equals denotes variable assignment. The double-equals is an operator with low precedence, which means you can do math and other operators with additional parenthesis (i.e., "a == b+1" means "a == (b+1)" not "(a == b)+1"). Other useful operators are != for not equal (the exclamation mark means "not" in general), < for "less than," for "greater than," and >= and <= for greater or less than or equal to. You can also use !< and !> for not less and not greater than. There are also other logical operators which let you use the logical contructs and, or, and not. Use the exclamation point for "not," so !(a == b) means a != b. Use the ampersand for "and," so (a == b) & (b == c) means "both a equals b and b equals c." Use the pipe for "or," so (a == 1) | (a == 2) means "a equals 1 or a equals 2." These operators are not short-circuit (see the reference page on short circuit operators for more on this). One more word about conditions. False is represented as the number 0. True is anything else. So if ("hi") works the same way as if 1. Also, this means that conditions can be stored in a variable as a number; cond = a == 16 will make cond 1 if a equals 16, 0 if it doesn't.

You can also have a statement (or block of statements) execute if the statement isn't true, as the following example illustrates:
def main()
{
	data = input();
	if (data == 0)
		println("zero!");
	else
		println("not zero!");
}

Looping

Most programs also contain some sort of loop. This is a statement (or block of statements) which gets exectued many times.

The while loop executes statements as long as some condition is true. It is used as the following code illustrates:
def main()
{
	data = 1;
	while (data) {
		data = input();
		println("got your input.");
	}
}

This executes the blocked lines until the input is 0. With while, the condition is evaluated first, so if it is initially false, the loop will never execute. That is why we had to put "data = 1;" before the while -- putting "data = 0;" would cause the loop to never execute. In this scenerio, it would be more appropriate to use the while in the following form:
def main()
{
	do {
		data = input();
		println("got your input.");
	} while (data)
}

As you can see, if you preceed the block (or statement) with the word "do," you can up the while after the block, and this postpones the condition check until after the block executes.

Another kind of looping comes from the for construct. You use this to make incremental loops, or loops where in each pass a variable is changed in some way. The simplest example follows:
def main()
{
	for(k in 1..10)
		println(k);
}

This will print the integers from 1 to 10 on the screen. You give for a variable first, in this example it is k. Then put the word "in" and then a range for the variable to be in. The loop executes, first with the variable equal to the beginning of the range, then incrementing the value by 1 every time the loop finishes until it has reached the end of the range. You can make the range from high to low to count backward, so changing the "for" line in the above example to for(k in 10..1) would print the numbers from 10 down to 1. The for construct also has uses in lists, which are covered in that part of the Tutorial.

The simplest use of the for loop doesn't use variables at all. If you supply an integer in the for parentheses, the block is executed that many times.

The next section in the Tutorial covers lists.
Web page maintained by Jason Cohen